import {AsyncStorage} from 'react-native';

class Storage{
    static get(key:string, cb: (error?: Error, result?: string) => void) {
        AsyncStorage.getItem(key, (err, obj) => {
            if (obj) {
                cb(err, JSON.parse(obj));
            } else {
                cb(err)
            }
        })
    }

    static async getAsync(key: string) {
        let res = await AsyncStorage.getItem(key);
        return res;
    }

    static set(key:string, value: string|Object, cb?: any) {
        if (value) {
            if (typeof value == 'string') {
                return AsyncStorage.setItem(key, value);
            }
            return AsyncStorage.setItem(key, JSON.stringify(value), cb)
        }
    }

    static update(key:string, value: string|Object){
        Storage.set(key, value);
    }

    static delete(key: string) {
        return AsyncStorage.removeItem(key);
    }

    static clear() {
        AsyncStorage.clear();
    }
}

export default Storage;